﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Ext.Net;
using System.Collections.Generic;

public partial class Popup_Popup_Eqp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SetListViewData(null, null);
        }
    }

    #region == ListView1_SelectionChange(업체선택) ==
    protected void ListView1_SelectionChange(object sender, DirectEventArgs e)
    {
        string[] strParams = this.GetJsonToString(e.ExtraParams["Values"]);

        if (strParams.Length > 1)
        {
            this.hdREGNO.Text = strParams[1];
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                DataSet ds = oService.Select_D_DAYWRITE_POP3_B_ITEM_S(Session["PROJ_CD"].ToString(), this.hdREGNO.Text, this.tbxMat.Text);

                this.Store2.RemoveAll();

                this.Store2.DataSource = ds.Tables[0];
                this.Store2.DataBind();
            }
        }
    }
    #endregion

    #region == 업체명 리스트뷰 바인드 ==
    protected void SetListViewData(object sender, StoreRefreshDataEventArgs e)
    {
        try
        {
            //프로젝트 코드 가져오기
            string strProjCd = string.Empty;
            if (Session["PROJ_CD"] != null)
            {
                strProjCd = Session["PROJ_CD"].ToString();
            }

            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                DataSet ds = oService.Select_D_DAYWRITE_POP3_B_CUST_S(strProjCd, this.tbxCustomer.Text);
                if (ds != null)
                {
                    List<object> data = new List<object>(ds.Tables[0].Rows.Count);
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        data.Add(new
                        {
                            BIZ_REGNO = dr["BIZ_REGNO"].ToString(),
                            COMP_NM = dr["COMP_NM"].ToString(),
                            REPR_NM = dr["REPR_NM"].ToString(),
                        });
                    }

                    this.ListView1.SelectedRow = null;
                    var store = this.ListView1.GetStore();
                    store.DataSource = data;
                    store.DataBind();

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        this.ListView1.Select(new int[] { 0 });
                    }

                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion


    protected void CreateParameter(object sender, DirectEventArgs e)
    {
        string json = e.ExtraParams["Values"];

        Dictionary<string, string>[] dicValues = JSON.Deserialize<Dictionary<string, string>[]>(json);
        string strVal = string.Empty;

        foreach (Dictionary<string, string> dic in dicValues)
        {
            foreach (KeyValuePair<string, string> row in dic)
            {
                strVal += row.Value + "|";
            }
        }

        strVal = strVal.Remove(strVal.Length - 1);
        hdRowValue.Text = strVal;
    }


    #region == Jason To String ==
    /// <summary>
    /// Json을 string[]으로...
    /// </summary>
    /// <param name="pJson"></param>
    /// <returns></returns>
    private string[] GetJsonToString(string pJson)
    {
        return pJson.Replace("[{", "").Replace(":", "").Replace(",", "").Replace("}]", "").Split(new string[] { "\"" }, StringSplitOptions.RemoveEmptyEntries);
    }
    #endregion


    #region 조회
    protected void PopListSearch(object sender, DirectEventArgs e)
    {
        SetGrid();
    }


    protected void SetGrid()
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            DataSet ds = null;

            if (!string.IsNullOrEmpty(this.tbxMat.Text))
            {
                this.Store1.RemoveAll();
                this.Store2.RemoveAll();

                ds = oService.Select_D_DAYWRITE_POP3_B_CUST_S(Session["PROJ_CD"].ToString(), this.tbxCustomer.Text);
                this.Store1.DataSource = ds.Tables[0];
                this.Store1.DataBind();

                if (string.IsNullOrEmpty(this.tbxCustomer.Text))
                    this.hdREGNO.Text = "";

                ds = oService.Select_D_DAYWRITE_POP3_B_ITEM_S(Session["PROJ_CD"].ToString(), this.hdREGNO.Text, this.tbxMat.Text);
                this.Store2.DataSource = ds.Tables[0];
                this.Store2.DataBind();
            }
            else
            {
                ds = oService.Select_D_DAYWRITE_POP3_B_CUST_S(Session["PROJ_CD"].ToString(), this.tbxCustomer.Text);
                this.Store1.DataSource = ds.Tables[0];
                this.Store1.DataBind();

                this.Store2.RemoveAll();
            }

            this.Store1.DataSource = ds;
            this.Store1.DataBind();
        }
    }
    #endregion
}
